package cn.usian.controller;

import cn.usian.pojo.Order;
import cn.usian.pojo.User;
import cn.usian.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("{id}")
    public Order  findById(@PathVariable long id){
        Order order = orderService.findById(id);
        //创建远程服务的地址
        //String url="http://localhost:8082/user/"+order.getUserId();
        String url="http://userservice/user/"+order.getUserId();
        //远程调用
        User user = restTemplate.getForObject(url, User.class);
        //将user对象保存在order对象中
        order.setUser(user);
        return order;
    }
}
